.PHONY: build clean test run help fmt vet lint swagger deps db db-model docker-up docker-down dev prod vendor generate generate-table

# 设置 Go 命令
GOCMD=go
GOBUILD=$(GOCMD) build
GOTEST=$(GOCMD) test
GOFMT=$(GOCMD) fmt
GOVET=$(GOCMD) vet
GOMOD=$(GOCMD) mod
GOGET=$(GOCMD) get

# 设置项目相关变量
APP_NAME=5title_gin
MAIN_PATH=./cmd/5title_gin
BUILD_DIR=./build
DOCKER_COMPOSE=docker-compose
GENERATOR_PATH=./cmd/generator

# 默认目标
.DEFAULT_GOAL := help

# 构建应用
build:
	@echo "构建应用..."
	@mkdir -p $(BUILD_DIR)
	$(GOBUILD) -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH)

# 运行应用(开发环境)
dev:
	@echo "运行开发环境..."
	GO_ENV=dev $(GOCMD) run $(MAIN_PATH)/main.go

# 运行应用(生产环境)
prod:
	@echo "运行生产环境..."
	GO_ENV=prod $(GOCMD) run $(MAIN_PATH)/main.go -env=prod

# 使用vendor运行
run-vendor:
	@echo "使用vendor运行应用..."
	$(GOCMD) run -mod=vendor $(MAIN_PATH)/main.go

# 清理构建产物
clean:
	@echo "清理构建产物..."
	@rm -rf $(BUILD_DIR)
	@go clean -testcache

# 运行测试
test:
	@echo "运行测试..."
	$(GOTEST) -v ./...

# 格式化代码
fmt:
	@echo "格式化代码..."
	$(GOFMT) ./...

# 运行 go vet
vet:
	@echo "运行 go vet..."
	$(GOVET) ./...

# 更新依赖
deps:
	@echo "更新依赖..."
	$(GOMOD) tidy
	$(GOMOD) download

# 创建vendor目录
vendor:
	@echo "创建vendor目录..."
	$(GOMOD) vendor

# 检查依赖完整性
deps-check:
	@echo "检查依赖完整性..."
	$(GOCMD) list -m all

# 数据库迁移(全局)
db:
	@echo "执行数据库全局迁移..."
	$(GOCMD) run $(MAIN_PATH)/main.go -db

# 数据库迁移(指定模型)
db-model:
	@echo "请输入要迁移的模型名称(多个模型用逗号分隔):"
	@read model_name; \
	echo "执行数据库模型迁移: $$model_name"; \
	$(GOCMD) run $(MAIN_PATH)/main.go -db -model $$model_name

# 生成 Swagger 文档
swagger:
	@echo "生成 Swagger 文档..."
	swag init

# 启动 Docker 容器
docker-up:
	@echo "启动 Docker 容器..."
	cd docker && $(DOCKER_COMPOSE) up -d

# 停止 Docker 容器
docker-down:
	@echo "停止 Docker 容器..."
	cd docker && $(DOCKER_COMPOSE) down

# 运行代码生成器(所有表)
generate:
	@echo "运行代码生成器..."
	cd $(GENERATOR_PATH) && $(GOCMD) run *.go

# 运行代码生成器(指定表)
generate-table:
	@echo "请输入要生成代码的表名:"
	@read table_name; \
	echo "为表 $$table_name 生成代码..."; \
	cd $(GENERATOR_PATH) && $(GOCMD) run *.go -table $$table_name

# 帮助信息
help:
	@echo "可用的命令:"
	@echo "  make build       - 构建应用"
	@echo "  make dev         - 开发环境运行应用"
	@echo "  make prod        - 生产环境运行应用"
	@echo "  make db          - 执行数据库全局迁移"
	@echo "  make db-model    - 执行数据库指定模型迁移"
	@echo "  make swagger     - 生成 Swagger 文档"
	@echo "  make run-vendor  - 使用vendor运行应用"
	@echo "  make deps        - 更新依赖"
	@echo "  make clean       - 清理构建产物"
	@echo "  make test        - 运行测试"
	@echo "  make fmt         - 格式化代码"
	@echo "  make vet         - 运行 go vet"
	@echo "  make vendor      - 创建vendor目录"
	@echo "  make deps-check  - 检查依赖完整性"
	@echo "  make docker-up   - 启动 Docker 容器"
	@echo "  make docker-down - 停止 Docker 容器"
	@echo "  make generate      - 运行代码生成器(所有表)"
	@echo "  make generate-table - 运行代码生成器(指定表)"